package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.ApprovalGroupData;
import com.lzx.iteam.bean.AttendanceData;
import com.lzx.iteam.bean.CloudGroup;
import com.lzx.iteam.util.Constants;

import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by Megan on 16/4/9.
 */
public class AttendanceTeamAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Object> attendanceDatas;
    private int mType; // 0:考勤  1:审批 2:周报

    public AttendanceTeamAdapter(Context mContext) {
        this.mContext = mContext;
    }

    /*public void bindData(ArrayList<ApprovalGroupData> attendanceDatas,int type) {
        this.attendanceDatas = attendanceDatas;
        this.mType = type;
        notifyDataSetChanged();
    }*/

    public void bindData(ArrayList<Object> attendanceDatas,int type) {
        this.attendanceDatas = attendanceDatas;
        this.mType = type;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return attendanceDatas==null?0:attendanceDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return attendanceDatas==null?null:attendanceDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.attendance_team_list_item_layout, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.tv_attendance_team_list_item_text);
            holder.textCount = (TextView) convertView.findViewById(R.id.approval_item_red_dot);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Object object = attendanceDatas.get(position);
        if (object instanceof ApprovalGroupData){
            holder.text.setText(((ApprovalGroupData)object).getGroup_name());
            setNewMsg(holder.textCount,((ApprovalGroupData)object).getTodo_count());
        }else{
            holder.text.setText(((AttendanceData)object).getGroupName());
            setNewMsg(holder.textCount,((AttendanceData)object).getUnreadCount());
        }
        /*if (mType == 1){
        }else if (mType == 2){
            setNewMsg(holder.textCount,attendanceDatas.get(position).getUnreadCount());

        }*/
        return convertView;
    }

    class ViewHolder {
        TextView text ,textCount;
    }
    public void setNewMsg(TextView mTvApprovalCount,int status){
        if (status == 0) {
            mTvApprovalCount.setVisibility(View.GONE);
        } else {
            mTvApprovalCount.setVisibility(View.VISIBLE);
            if (status > 0 && status < 10) {
                ViewGroup.LayoutParams linearParams = mTvApprovalCount.getLayoutParams();
                int pd = Constants.dip2px(mContext, 16);
                linearParams.width = pd;
                linearParams.height = pd;
                mTvApprovalCount.setLayoutParams(linearParams);
                mTvApprovalCount.setBackgroundResource(R.drawable.unread_1);
                mTvApprovalCount.setGravity(Gravity.CENTER);
                mTvApprovalCount.setText(status + "");
            } else if (status >= 10 && status < 100) {
                ViewGroup.LayoutParams linearParams = mTvApprovalCount.getLayoutParams();
                int widthPx = Constants.dip2px(mContext, 24);
                int heightPx = Constants.dip2px(mContext, 16);
                linearParams.width = widthPx;
                linearParams.height = heightPx;
                mTvApprovalCount.setLayoutParams(linearParams);
                mTvApprovalCount.setBackgroundResource(R.drawable.unread_2);
                mTvApprovalCount.setText(status + "");
            } else if (status >= 100) {
                ViewGroup.LayoutParams linearParams = mTvApprovalCount.getLayoutParams();
                int widthPx = Constants.dip2px(mContext, 34);
                int heightPx = Constants.dip2px(mContext, 16);
                linearParams.width = widthPx;
                linearParams.height = heightPx;
                mTvApprovalCount.setLayoutParams(linearParams);
                mTvApprovalCount.setBackgroundResource(R.drawable.unread_3);
                mTvApprovalCount.setText("99+");
            }
        }
    }
}
